import { getAuthorization } from '@/common/request/api'
import { setRequestBaseURL,setIMRequestBaseURL } from '@/common/request'
import { develoDomain } from '@/env'
import {getWebBrowser} from '@/common/utils'

async function getBaseUrl() {
	const res = await uni.request({
		url: `${location.origin}/api.json`
	})
	
	if (res.statusCode === 200) {
		setRequestBaseURL(res.data[0] + '/')
		res.data[2] && setIMRequestBaseURL(res.data[2] + '/')
	}
}


export default  async () => {
	
	if (process.env.NODE_ENV === 'production') {
		await getBaseUrl()
	}
	
	const initRes = await getAuthorization({
		domain: process.env.NODE_ENV === 'development' ? develoDomain : location.hostname
	})
	document.title = initRes.company
	const titleView = document.querySelector('title') as HTMLTimeElement
	titleView.innerText = initRes.company
	setTimeout(() => {
		titleView.innerText = initRes.company
	}, 500)
	uni.setStorageSync('tenant', initRes.data)
	uni.setStorageSync('appCompanyName', initRes.company)
	if (initRes.logo) {
		uni.setStorageSync('logo', initRes.logo)
	}
	if (getWebBrowser('wx')) {
		initRes.domainMp && (window.location.href = `${initRes.domainMp}/#${location.hash.split('#')[1]}`)
	}
	window.onbeforeunload = function() {
		uni.removeStorageSync('userSalt')
	}
}